package org.scboy.rbac.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.cache.SystemConfig;
import org.scboy.common.request.Result;
import org.scboy.rbac.service.LoginSessionService;
import org.scboy.rbac.service.PermissionService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("rbac/permission")
@Menu("权限管理系统.权限管理")

public class PermissionController {
	
	@Autowired
	PermissionService permissionService;
	
	@Autowired
	LoginSessionService loginSessionService;

	@Autowired
	SystemConfig config;
	
	/**
	 * 分页查询权限列表
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Permission("分页查询权限列表")
	public Result page(@RequestBody Model params){
		Page page=permissionService.page(params);
		return Result.Success(page.getData(),page.getCount());	
	}
	
	/**
	 * 新增权限
	 * @param request
	 * @return
	 */
	@PostMapping("add")
	@Permission("新增权限")
	public Result add(@RequestBody Model params){
		if(permissionService.add(params)){
			return Result.Success("RBAC_PERMISSION_ADD_SUCCESS");
		}else{
			return Result.Error("RBAC_PERMISSION_ADD_ERROR");
		}	
	}
	
	/**
	 * 更新权限
	 * @param request
	 * @return
	 */
	@PutMapping("update")
	@Permission("更新权限")
	public Result update(@RequestBody Model params){
		if(permissionService.update(params)){
			return Result.Success("RBAC_PERMISSION_UPDATE_SUCCESS");
		}else{
			return Result.Error("RBAC_PERMISSION_UPDATE_ERROR");
		}
	}
	
	/**
	 * 删除权限
	 * @param request
	 * @return
	 */
	@DeleteMapping("delete")
	@Permission("删除权限")
	public Result delete(String id){
		if(permissionService.delete(id)){
			return Result.Success("RBAC_PERMISSION_DELETE_SUCCESS");
		}else{
			return Result.Error("RBAC_PERMISSION_DELETE_ERROR");
		}
	}
	
	/**
	 * 启用禁用权限
	 * @param request
	 * @param id
	 * @return
	 */
	@PutMapping("checkstate")
	@Permission("启用禁用权限")
	public Result checkState(String id){
		if(permissionService.checkState(id)){
			return Result.Success("RBAC_PERMISSION_CHECKSTATE_SUCCESS");
		}else{
			return Result.Error("RBAC_PERMISSION_CHECKSTATE_ERROR");
		}	
	}
	
	/**
	 * 获取权限详情
	 * @param request
	 * @param id
	 * @return
	 */
	@GetMapping("get")
	@Permission("获取权限信息")
	 public Result get(String id){
		return Result.Success(permissionService.get(id), "");	
	}
	
	@GetMapping("route")
	@Permission("获取权限信息")
	 public Result route(HttpServletRequest request,String systemId){
		String token=request.getHeader(config.getConfig("TOKEN_KEY"));
		return Result.Success(permissionService.route(systemId,token), "");	
	}
	
	
}
